//
//  BalanceCashViewController.m
//  danpin
//
//  Created by chuangjia on 7/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "BalanceCashViewController.h"
#import "NSString+Extensions.h"
#import "TTTAttributedLabel.h"
#import "CalcLabel.h"
@interface BalanceCashViewController ()<UITextFieldDelegate>
{
    
}
@property(nonatomic,strong) UITextField     *txtUser;
@property(nonatomic,strong) UITextField     *txtUserOk;
@property(nonatomic,strong) UITextField     *txtName;
@property(nonatomic,strong) UITextField     *txtPass;
@property(nonatomic,strong) UITextField     *txtPrice;
@property(nonatomic, strong) TTTAttributedLabel* ttitle;

@end

@implementation BalanceCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"提现";
    self.rightTitle =@"确定";
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
    self.txtName=nil;
    self.txtPass=nil;
    self.txtPrice=nil;
    self.txtUserOk=nil;
    self.txtUser=nil;
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            if ([self isLogin]) {
                 [self tixian];
            }
        }
            break;
        default:
            break;
    }
}
-(void)initUI
{
    
    self.txtUser.frame =CGRectMake(PHOTO_FRAME_WIDTH, 80, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*3);
    self.txtUserOk.frame =CGRectMake(PHOTO_FRAME_WIDTH, 120, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*3);
    self.txtName.frame =CGRectMake(PHOTO_FRAME_WIDTH, 160, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*3);
    self.txtPass.frame =CGRectMake(PHOTO_FRAME_WIDTH, 200, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*3);
    self.txtPrice.frame =CGRectMake(PHOTO_FRAME_WIDTH, 240, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*3);
    NSString *cstr =@"备注:\n1.  账户余额没有任何使用限制，可以抵扣货商品价格、运费、关税等；\n2.  提现请输入支付宝账号，不支持银行卡号；\n3.  支付宝若填写错误，客服将人工联系修改，请谨慎填写；\n4.  提现需3~5个工作日到账，目前暂无手续费；";
    [self.ttitle setText:cstr afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
     {
         [attributedString addAttribute:NSFontAttributeName
                                  value:FONT(12)
                                  range:NSMakeRange(0,[attributedString length])];
         //设置字体颜色
         [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0,[attributedString length])];
         return attributedString;
         
     }];
    
    float fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*10) str:self.ttitle.attributedText];
    self.ttitle.frame = CGRectMake(PHOTO_FRAME_WIDTH, 280,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*2, fheight);
    
}
-(TTTAttributedLabel*)ttitle
{
    if (!_ttitle) {
        _ttitle =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*8,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH* 10,40)];
        _ttitle.backgroundColor = [UIColor clearColor];
        _ttitle.textColor = [UIColor lightGrayColor];
        _ttitle.textAlignment = NSTextAlignmentLeft;
        _ttitle.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _ttitle.numberOfLines=0;
        _ttitle.font = FONT(12);
        [self.view addSubview:_ttitle];
    }
    return _ttitle;
}
-(UITextField *)txtUser
{
    if (!_txtUser) {
        UILabel* left = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        left.textColor = TITLE_COLOR;
        left.font = FONT(13);
        left.text = @"  账号:  ";
        [left sizeToFit];
        
        self.txtUser = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtUser.textColor =[UIColor lightGrayColor];
        self.txtUser.leftViewMode = UITextFieldViewModeAlways;
        self.txtUser.leftView = left;
        self.txtUser.placeholder = @" 支付宝账号";
        [self.txtUser setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtUser.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUser.font = FONT(13);
        self.txtUser.keyboardType =UIKeyboardTypeDefault;
        self.txtUser.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtUser.returnKeyType = UIReturnKeyNext;
        self.txtUser.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUser.delegate=self;
        [self.view addSubview:self.txtUser];
    }
    return _txtUser;
}
-(UITextField *)txtUserOk
{
    if (!_txtUserOk) {
        UILabel* left = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        left.textColor = TITLE_COLOR;
        left.font = FONT(13);
        left.text = @"  确认账号:  ";
        [left sizeToFit];
        
        self.txtUserOk = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtUserOk.textColor =[UIColor lightGrayColor];
        self.txtUserOk.leftViewMode = UITextFieldViewModeAlways;
        self.txtUserOk.leftView = left;
        self.txtUserOk.placeholder = @" 再次输入支付宝账号";
        [self.txtUserOk setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtUserOk.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUserOk.font = FONT(13);
        self.txtUserOk.keyboardType =UIKeyboardTypeDefault;
        self.txtUserOk.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtUserOk.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtUserOk.returnKeyType = UIReturnKeyNext;
        self.txtUserOk.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUserOk.delegate=self;
        [self.view addSubview:self.txtUserOk];
    }
    return _txtUserOk;
}
-(UITextField *)txtName
{
    if (!_txtName) {
        UILabel* left = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        left.textColor = TITLE_COLOR;
        left.font = FONT(13);
        left.text = @"  姓名:  ";
        [left sizeToFit];
        
        self.txtName = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtName.textColor =[UIColor lightGrayColor];
        self.txtName.leftViewMode = UITextFieldViewModeAlways;
        self.txtName.leftView = left;
        self.txtName.placeholder = @" 绑定支付宝的名称";
        [self.txtName setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtName.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtName.font = FONT(13);
        self.txtName.keyboardType =UIKeyboardTypeDefault;
        self.txtName.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtName.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtName.returnKeyType = UIReturnKeyNext;
        self.txtName.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtName.delegate=self;
        [self.view addSubview:self.txtName];
    }
    return _txtName;
}
-(UITextField *)txtPass
{
    if (!_txtPass) {
        UILabel* left = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        left.textColor = TITLE_COLOR;
        left.font = FONT(13);
        left.text = @"  联系方式:  ";
        [left sizeToFit];
        
        self.txtPass = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtPass.textColor =[UIColor lightGrayColor];
        self.txtPass.leftViewMode = UITextFieldViewModeAlways;
        self.txtPass.leftView = left;
        self.txtPass.placeholder = @" 手机号";
        [self.txtPass setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtPass.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPass.font = FONT(13);
        self.txtPass.keyboardType =UIKeyboardTypeDefault;
        self.txtPass.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtPass.returnKeyType = UIReturnKeyNext;
        self.txtPass.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPass.delegate=self;
        [self.view addSubview:self.txtPass];
    }
    return _txtPass;
}

-(UITextField *)txtPrice
{
    if (!_txtPrice) {
        NSMutableDictionary *userinfo = (NSMutableDictionary*)[ZShare sharedInstance].userInfo;
        NSString *user_balance =[[NSString stringWithFormat:@"%@",[userinfo objectForKey:@"user_balance"]] replaceNullString];
        UILabel* left = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        left.textColor = TITLE_COLOR;
        left.font = FONT(13);
        left.text = @"  金额:  ";
        [left sizeToFit];
        self.txtPrice = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtPrice.textColor =[UIColor lightGrayColor];
        self.txtPrice.leftViewMode = UITextFieldViewModeAlways;
        self.txtPrice.leftView = left;
        if (user_balance.length>0) {
            self.txtPrice.placeholder = [NSString stringWithFormat:@" 提现金额(最多 %@)",user_balance];
        }else{
             self.txtPrice.placeholder = @" 提现金额";
        }
       
        [self.txtPrice setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtPrice.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPrice.font = FONT(13);
        self.txtPrice.keyboardType =UIKeyboardTypeDefault;
        self.txtPrice.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtPrice.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtPrice.returnKeyType = UIReturnKeyDone;
        self.txtPrice.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPrice.delegate=self;
        [self.view addSubview:self.txtPrice];
    }
    return _txtPrice;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField==self.txtUser) {
        [self.txtUserOk becomeFirstResponder];
    }else if (textField==self.txtUserOk) {
        [self.txtName becomeFirstResponder];
    }else if (textField==self.txtName){
        [self.txtPass becomeFirstResponder];
    }else if (textField==self.txtPass){
        [self.txtPrice resignFirstResponder];
    }else if (textField==self.txtPrice){
        if ([self isLogin]) {
            [self tixian];
        }
        [textField resignFirstResponder];
    }else
    {
        [textField resignFirstResponder];
    }
    return YES;
}
//=======================================================
// 默认地址
//=======================================================
-(void)tixian
{
    /*
     接口参数 action = applyForCash
     
     业务参数 fee		//提现金额
     phone		//手机号
     real_name	//真实姓名
     alipay_account	//支付宝账号
     */
    if (self.txtUser.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请输入支付宝账号"];
        return;
    }
    if (self.txtUserOk.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请再次输入支付宝账号，进行确认！"];
        return;
    }
    if(![self.txtUserOk.text isEqualToString:self.txtUser.text])
    {
        [[ZToastManager ShardInstance] showtoast:@"两次账号不同，请确认支付宝账号是否正确！"];
        return;
    }
    if (self.txtName.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请输入联系人姓名"];
        return;
    }
    if (self.txtPass.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请输入联系方式"];
        return;
    }
    if (self.txtPrice.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请输入提现金额"];
        return;
    }
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setValue:self.txtUser.text forKey:@"alipay_account"];
    [dictionary setValue:self.txtName.text forKey:@"real_name"];
    [dictionary setValue:self.txtPass.text forKey:@"phone"];
    [dictionary setValue:self.txtPrice.text forKey:@"fee"];
    
     [[ZToastManager ShardInstance] showprogress];
    __weak typeof (self) wSelf = self;
    [OrderApi applyForCash:dictionary success:^(AFHTTPRequestOperation *operation, id result) {
        [[ZToastManager ShardInstance] hideprogress];
        if (result&&[result isKindOfClass:[NSDictionary class]]) {
            NSDictionary *resultDist =(NSDictionary*)result;
           [[ZToastManager ShardInstance] showtoast:[resultDist objectForKey:@"msg"]];
//            NSLog(@"%@",resultDist);
            int code=  [[resultDist objectForKey:@"code"] intValue];
            if (code==1) {

                if (wSelf.navigationController) {
                    if (wSelf.navigationController.viewControllers.count == 1) {
                        [wSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
                    } else {
                        [wSelf.navigationController popViewControllerAnimated:YES];
                    }
                } else {
                    [wSelf dismissViewControllerAnimated:YES completion:nil];
                }
            }
            
        }
    
    } failure:^(NSError *error) {
       [[ZToastManager ShardInstance] hideprogress];
    }];
}
@end
